const app = getApp()
import { getAllUnReadMessageCount, getMessageList, setDesignerMessageRead, postInviteProcess } from '../../../api/index'
import { formatTimeTwo } from '../../../utils/util'

Page({
  data: {
    tabs: [
      { label: '全部消息', value: 0, count: 0 },
      { label: '系统消息', value: 1, count: 0 },
      { label: '材料消息', value: 2, count: 0 },
      { label: '入册消息', value: 3, count: 0 },
      { label: '消息公告', value: 5, count: 0 }
    ],
    tabActive: 0,
    isEmpty: false,
    currentPage: 1,
    pageSize: 15,
    list: [],
    tabHeight: 0,
    showMsgModal: false,
    currentItem: {}
  },
  selectTab ({ currentTarget: { dataset: { item } } }) {
    this.setData({ tabActive: item.value })
    this.getList()
  },
  async seeDetail ({ currentTarget: { dataset: { item, index } } }) {
    try {
      if (!item.isRead) {
        let query = { messageId: item.id }
        if (item.type === 5) query.type = 1
        await setDesignerMessageRead({ ...query })
        this.getCount()
        this.data.list[index].isRead = item.isRead ? false : true
        let key = `list[${index}].isRead`
        this.setData({ [key]: this.data.list[index].isRead })
        if (item.type === 5) {
          wx.$nav.navigateTo(`/pages/account/messageDetail/index?id=${item.id}&type=${item.type}`)
        } else {
          if (item.subType === 1) {
            if (item.isAgree !== true && item.isAgree !== false) {
              const { confirm } = await wx.$pro.showModal({ title: item.typeStr, content: item.detail, cancelText: '拒绝', confirmText: '同意', confirmColor: '#FF2E42' })
              let isAgree = false
              if (confirm) isAgree = true
              else isAgree = false
              await postInviteProcess({ teamId: item.objectId, isAgree: isAgree, messageId: item.id })
              this.data.list[index].isAgree = isAgree
              let key = `list[${index}].isAgree`
              this.setData({ [key]: this.data.list[index].isAgree })
            } else {
              this.setData({ showMsgModal: true, currentItem: item })
            }
          } else {
            this.setData({ showMsgModal: true, currentItem: item })
          }
        }
      } else {
        if (item.type === 5) {
          wx.$nav.navigateTo(`/pages/account/messageDetail/index?id=${item.id}&type=${item.type}`)
        } else {
          if (item.subType === 1) {
            if (item.isAgree !== true && item.isAgree !== false) {
              const { confirm } = await wx.$pro.showModal({ title: item.typeStr, content: item.detail, cancelText: '拒绝', confirmText: '同意', confirmColor: '#FF2E42' })
              let isAgree = false
              if (confirm) isAgree = true
              else isAgree = false
              await postInviteProcess({ teamId: item.objectId, isAgree: isAgree, messageId: item.id })
              this.data.list[index].isAgree = isAgree
              let key = `list[${index}].isAgree`
              this.setData({ [key]: this.data.list[index].isAgree })
            } else {
              this.setData({ showMsgModal: true, currentItem: item })
            }
          } else {
            this.setData({ showMsgModal: true, currentItem: item })
          }
        }
      }
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  close () {
    this.setData({ showMsgModal: false, currentItem: {} })
  },
  async getCount () {
    try {
      const { ResultSet } = await getAllUnReadMessageCount()
      ResultSet.userData.data.forEach(item => {
        this.data.tabs.forEach(row => {
          if (item.type === row.value) row.count = item.unReadCount
        })
      })
      this.setData({ tabs: this.data.tabs })
      let query = this.createSelectorQuery()
      query.select('.tag-list').boundingClientRect(rect=>{
        let height = rect.height
        this.setData({ tabHeight: height })
      }).exec()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = true
      const { ResultSet } = await getMessageList({ currentPage: this.data.currentPage, pageSize: this.data.pageSize, type: this.data.tabActive })
      this.alloaded = !ResultSet.userData.data.length
      this.data.isEmpty = !ResultSet.userData.data.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.data.length) {
        this.data.currentPage = this.data.currentPage + 1
        this.setData({
          currentPage: this.data.currentPage
        })
        this.setList(ResultSet.userData.data)
      }
      this.loading = false
    } catch (err) {
      console.log(err)
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  setList (arr) {
    arr.forEach(item => {
      item.createTime = formatTimeTwo(item.createTime, 'Y-M-D h:m:s')
    })
    this.data.list.push(...arr)
    this.setData({ list: this.data.list })
  },
  async getList () {
    try {
      this.setData({ list: [] })
      let query = {
        currentPage: 1,
        pageSize: this.data.pageSize,
        type: this.data.tabActive
      }
      wx.$pro.showLoading({ title: '加载中...', mask: true })
      const { ResultSet } = await getMessageList({ ...query })
      this.loading = false
      this.alloaded = false
      if (ResultSet.userData.data.length) this.data.currentPage = 2
      this.data.isEmpty = ResultSet.userData.data.length && ResultSet.userData.data.length < this.data.pageSize
      this.setData({
        currentPage: this.data.currentPage,
        isEmpty: this.data.isEmpty
      })
      this.setList(ResultSet.userData.data)
      this.loading = false
      wx.$pro.hideLoading()
    } catch (err) {
      wx.$pro.hideLoading()
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  onShow () {
    this.getCount()
    this.getList()
  }
})